﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace Exact_Extension
{
    public partial class MsgShortFixedTerm : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        ISHCMC_V_StudentListWithDebtor itemStudent;
        Common common;
        string schoolyear;
        DateTime InvoiceDate;
        DateTime DueDate;
        DateTime EnrollmentDate;
        IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
        public MsgShortFixedTerm(ExactDataContext _DS , ISHCMC_V_StudentListWithDebtor _itemStudent,Common _common
            ,string _schoolyear,DateTime _InvoiceDate,DateTime _DueDate)
        {
            InitializeComponent();
            DS = _DS;
            itemStudent = _itemStudent;
            this.LoadInitialization();
            schoolyear = _schoolyear;
            InvoiceDate = _InvoiceDate;
            DueDate = _DueDate;
            common = _common;
            EnrollmentDate = Convert.ToDateTime("01/08/" + DateTime.Now.Year.ToString(), culture);
        }
        private void LoadInitialization()
        {
            var queryTerm = from Term in DS.ISHCMC_FixedTerms
                            select Term;
            this.iSHCMCFixedTermBindingSource.DataSource = queryTerm;
        }

        private void btnInvoice_Click(object sender, EventArgs e)
        {
            DialogResult ResultConfirm = MessageBox.Show("Are your sure to make invoice", "Confirmation", MessageBoxButtons.OKCancel);
            if (ResultConfirm == DialogResult.OK)
            {
                common.UpdateInvoiceFixTerm(itemStudent);
                ISHCMC_Section itemSec = new ISHCMC_Section();

                string termcode = "";

                if (this.lookUpEdit_ShortFixedTerm.EditValue != null)
                {
                    termcode = this.lookUpEdit_ShortFixedTerm.EditValue.ToString();
                }

                if (itemStudent.PaymentMethod == null)
                {
                    itemSec = common.GetSection("A", schoolyear, itemStudent.School, DueDate);
                }
                else
                {
                    itemSec = common.GetSection(itemStudent.PaymentMethod, schoolyear, itemStudent.School, DueDate);
                }
                if (itemStudent.StudentID != null)
                {

                    if (itemStudent.EnrollmentDate >= EnrollmentDate || !itemStudent.EnrollmentDate.HasValue)
                    {

                        common.GenerateInvoice("APPF", itemStudent, schoolyear, InvoiceDate
                            , itemSec, true, termcode);
                    }
                    if (itemStudent.COE == 0 && itemStudent.COENumber == 0 && itemStudent.School == "ISHCMC")
                    {
                        if (!itemStudent.Grade.StartsWith("E") && !itemStudent.Grade.StartsWith("K"))
                        {

                            common.GenerateInvoice("ADF", itemStudent, schoolyear, InvoiceDate, itemSec, !this.chkLateEnrollment.Checked, termcode);

                        }

                    }
                    if (itemStudent.COE == 1 && itemStudent.COENumber <= 0)
                    {
                        common.GenerateInvoice("COE", itemStudent, schoolyear, InvoiceDate, itemSec, !this.chkLateEnrollment.Checked, termcode);
                    }

                    common.GenerateInvoice("TF", itemStudent, schoolyear, InvoiceDate, itemSec, !this.chkLateEnrollment.Checked, termcode);
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void chkLateEnrollment_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLateEnrollment.Checked)
            {
                this.lookUpEdit_ShortFixedTerm.Enabled = false;
            }
            else
            {
                this.lookUpEdit_ShortFixedTerm.Enabled = true;
            }
        }
    }
}